package com.wh6648.padclock;

import android.app.Activity;
import android.app.Service;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;

import java.util.ArrayList;
import java.util.List;


public class ClockAlarmActivity extends Activity {
    private MediaPlayer mediaPlayer;
    private Vibrator vibrator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock_alarm);
        String message = this.getIntent().getStringExtra("msg");
        int flag = this.getIntent().getIntExtra("flag", 0);
        showDialogInBroadcastReceiver(message, flag);
    }

    private void showDialogInBroadcastReceiver(String message, final int flag) {

        List<Integer> yinyueList = new ArrayList<>();
        yinyueList.add(R.raw.m01_caimogu);
        yinyueList.add(R.raw.m02_cuntzainali);
        yinyueList.add(R.raw.m03_fen_nu_de_xiao_niao);
        yinyueList.add(R.raw.m04_rangwomdangqiusuangjiang);
        yinyueList.add(R.raw.m05_saoxiandui);
        yinyueList.add(R.raw.m06_shuai_cong_ge_m);
        yinyueList.add(R.raw.m07_sisangziyoumamahao);
        yinyueList.add(R.raw.m08_xiaoxiaoqie);
        yinyueList.add(R.raw.m09_xuegusi);

        if (flag == 1 || flag == 2) {
            //result = (int)(33*Math.random()+1);//Math函数库重的random()方法可以产生0至1的随机数
            int r = (int) (yinyueList.size() * Math.random() + 1);
            if (r < 1) {
                r = 1;
            }
            if (r > yinyueList.size()) {
                r = yinyueList.size();
            }
            mediaPlayer = MediaPlayer.create(this, yinyueList.get(r - 1));
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        }

        //数组参数意义：第一个参数为等待指定时间后开始震动，震动时间为第二个参数。后边的参数依次为等待震动和震动的时间
        //第二个参数为重复次数，-1为不重复，0为一直震动
        if (flag == 0 || flag == 2) {
            vibrator = (Vibrator) this.getSystemService(Service.VIBRATOR_SERVICE);
            vibrator.vibrate(new long[]{100, 10, 100, 600}, 0);
        }

        final SimpleDialog dialog = new SimpleDialog(this, R.style.Theme_dialog);
        dialog.show();
        dialog.setTitle("闹钟提醒");
        dialog.setMessage(message);
        dialog.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.bt_confirm == v || dialog.bt_cancel == v) {
                    if (flag == 1 || flag == 2) {
                        mediaPlayer.stop();
                        mediaPlayer.release();
                    }
                    if (flag == 0 || flag == 2) {
                        vibrator.cancel();
                    }
                    dialog.dismiss();
                    finish();
                }
            }
        });


    }

}
